package com.itbaizhan.dao.impl;

import com.itbaizhan.dao.ItemDao;
import com.itbaizhan.pojo.Item;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

/**
 * @author spf
 * @date 2024/3/8
 * @time 9:26
 */
@Repository
public class ItemDaoImpl implements ItemDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public long update(String itemId, Boolean rented) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(itemId));

        Update update = Update.update("isRented", rented);

        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Item.class);
        return updateResult.getModifiedCount();
    }
}
